/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vietnamnet;

import DTO.TinTucDTO;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author Mr_Quay
 */
public class Xahoi {

    /**
     * @param args the command line arguments
     */
    // "//*[@id=\"BodyWraper\"]/div/div[1]/div[4]/div[1]/div[2]/div[4]/div/h2/a"
    public String getTitle(String xmlFile, String xPathTitle) throws ParserConfigurationException, SAXException, XPathExpressionException, IOException {
        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setNamespaceAware(true); // never forget this!
        DocumentBuilder builder = domFactory.newDocumentBuilder();
        Document doc = builder.parse(xmlFile);
        //m thử copy xpath thẻ <div> coi   
        XPathFactory factory = XPathFactory.newInstance();
        XPath xPath = factory.newXPath();
        XPathExpression xp = xPath.compile(xPathTitle);

        // <a>in cai nay </a>
        Object kq = xp.evaluate(doc, XPathConstants.NODE);
        Node nodes = (Node) kq;
        return nodes.getTextContent();

    }

    public String getLink(String xmlFile, String xPathLink) throws ParserConfigurationException, SAXException, XPathExpressionException, IOException {
        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setNamespaceAware(true); // never forget this!
        DocumentBuilder builder = domFactory.newDocumentBuilder();
        Document doc = builder.parse(xmlFile);
        //m thử copy xpath thẻ <div> coi   
        XPathFactory factory = XPathFactory.newInstance();
        XPath xPath = factory.newXPath();
        XPathExpression xp = xPath.compile(xPathLink);
        //xPath.setNamespaceContext(new MineralOccurrenceNamespaceContext());

        // <a href="http://abc.com.vn" name="aadad"></a>         
        Object kq = xp.evaluate(doc, XPathConstants.NODE);
        Element nodes = (Element) kq;
        return nodes.getAttribute("href");
    }

    public String getDate(String xmlFile, String xPathDate) throws ParserConfigurationException, SAXException, XPathExpressionException, IOException {
        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setNamespaceAware(true); // never forget this!
        DocumentBuilder builder = domFactory.newDocumentBuilder();
        Document doc = builder.parse(xmlFile);
        //m thử copy xpath thẻ <div> coi   
        XPathFactory factory = XPathFactory.newInstance();
        XPath xPath = factory.newXPath();
        XPathExpression xp = xPath.compile(xPathDate);

        // <a>in cai nay </a>
        Object kq = xp.evaluate(doc, XPathConstants.NODE);
        Node nodes = (Node) kq;
        return nodes.getTextContent();

    }

    public String getImage(String xmlFile, String xPathImage) throws ParserConfigurationException, SAXException, XPathExpressionException, IOException {
        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setNamespaceAware(true); // never forget this!
        DocumentBuilder builder = domFactory.newDocumentBuilder();
        Document doc = builder.parse(xmlFile);
        //m thử copy xpath thẻ <div> coi   
        XPathFactory factory = XPathFactory.newInstance();
        XPath xPath = factory.newXPath();
        XPathExpression xp = xPath.compile(xPathImage);
        //xPath.setNamespaceContext(new MineralOccurrenceNamespaceContext());

        // <a href="http://abc.com.vn" name="aadad"></a>         
        Object kq = xp.evaluate(doc, XPathConstants.NODE);
        Element nodes = (Element) kq;
        return nodes.getAttribute("src");
    }

    public String getTomtat(String xmlFile, String xPathTomtat) throws ParserConfigurationException, SAXException, XPathExpressionException, IOException {
        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setNamespaceAware(true); // never forget this!
        DocumentBuilder builder = domFactory.newDocumentBuilder();
        Document doc = builder.parse(xmlFile);
        //m thử copy xpath thẻ <div> coi   
        XPathFactory factory = XPathFactory.newInstance();
        XPath xPath = factory.newXPath();
        XPathExpression xp = xPath.compile(xPathTomtat);

        // <a>in cai nay </a>
        Object kq = xp.evaluate(doc, XPathConstants.NODE);
        Node nodes = (Node) kq;
        return nodes.getTextContent();

    }

    public int Count(String xmlFile, String xPathCha) throws ParserConfigurationException, SAXException, XPathExpressionException, IOException {
        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setNamespaceAware(true); // never forget this!
        DocumentBuilder builder = domFactory.newDocumentBuilder();
        Document doc = builder.parse(xmlFile);
        //m thử copy xpath thẻ <div> coi   
        XPathFactory factory = XPathFactory.newInstance();
        XPath xPath = factory.newXPath();
        XPathExpression xp = xPath.compile(xPathCha);

        Object kq = xp.evaluate(doc, XPathConstants.NODESET);
        NodeList nodes = (NodeList) kq;
        return nodes.getLength();

    }
    public List<DTO.TinTucDTO> LoadXahoi() throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {
        List<DTO.TinTucDTO> lst=new ArrayList<>();
        Xahoi xh=new Xahoi();
        int n = xh.Count("vnxahoi.xml", "//*[@id=\"BodyWraper\"]/div/div[1]/div[4]/div[1]/div[2]/*");

        for(int i=1;i<=15;i++){
            DTO.TinTucDTO dto=new TinTucDTO("http://vietnamnet.vn/vn/xa-hoi/");
            String xpathTitle = "//*[@id=\"BodyWraper\"]/div/div[1]/div[4]/div[1]/div[2]/div[" + i + "]/div/h2/a";
            String title=xh.getTitle("vnxahoi.xml", xpathTitle);
            dto.setTieude(title);
            
            String xpathLink = "//*[@id=\"BodyWraper\"]/div/div[1]/div[4]/div[1]/div[2]/div[" + i + "]/div/h2/a";
            String link = xh.getLink("vnxahoi.xml", xpathLink);
            dto.setLink(link);

            String xpathDate = "//*[@id=\"BodyWraper\"]/div/div[1]/div[4]/div[1]/div[2]/div[" + i + "]/div/p";
            String date = xh.getDate("vnxahoi.xml", xpathDate);
            dto.setNgaydang(date);

            String xpathImgage="//*[@id=\"BodyWraper\"]/div/div[1]/div[4]/div[1]/div[2]/div["+i+"]/div/a/img";
            String image=xh.getImage("vnxahoi.xml", xpathImgage);
            dto.setHinhanh(image);

            String xpathTomtat = "//*[@id=\"BodyWraper\"]/div/div[1]/div[4]/div[1]/div[2]/div[" + i + "]/div/h3";
            String tomtat = xh.getTomtat("vnxahoi.xml", xpathTomtat);
            dto.setTomtat(tomtat);
            lst.add(dto);
        }
        return lst;
        
   }
//    public void LoadXahoi() throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {
//        Xahoi xh=new Xahoi();
//        int n = xh.Count("vnxahoi.xml", "//*[@id=\"BodyWraper\"]/div/div[1]/div[4]/div[1]/div[2]/*");
//        System.out.println(n+"");
//        for(int i=1;i<=15;i++){
//            String xpathTitle = "//*[@id=\"BodyWraper\"]/div/div[1]/div[4]/div[1]/div[2]/div[" + i + "]/div/h2/a";
//            String title=xh.getTitle("vnxahoi.xml", xpathTitle);
//            System.out.println(i+"----Title:"+title);
//            
//            String xpathLink = "//*[@id=\"BodyWraper\"]/div/div[1]/div[4]/div[1]/div[2]/div[" + i + "]/div/h2/a";
//            String link = xh.getLink("vnxahoi.xml", xpathLink);
//            System.out.println("Link:" + link);
//
//            String xpathDate = "//*[@id=\"BodyWraper\"]/div/div[1]/div[4]/div[1]/div[2]/div[" + i + "]/div/p";
//            String date = xh.getDate("vnxahoi.xml", xpathDate);
//            System.out.println("----Date:" + date);
//
//            String xpathImgage="//*[@id=\"BodyWraper\"]/div/div[1]/div[4]/div[1]/div[2]/div["+i+"]/div/a/img";
//            String image=xh.getImage("vnxahoi.xml", xpathImgage);
//            System.out.println("----Image:"+image);
//
//            String xpathTomtat = "//*[@id=\"BodyWraper\"]/div/div[1]/div[4]/div[1]/div[2]/div[" + i + "]/div/h3";
//            String tomtat = xh.getTomtat("vnxahoi.xml", xpathTomtat);
//            System.out.println("----Tomtat:" + tomtat);
//        }
//   }
    
//    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {      
//        Xahoi xh=new Xahoi();
//        int n = xh.Count("vnxahoi.xml", "//*[@id=\"BodyWraper\"]/div/div[1]/div[4]/div[1]/div[2]/*");
//        System.out.println(n+"");
//        for(int i=1;i<=15;i++){
//            String xpathTitle = "//*[@id=\"BodyWraper\"]/div/div[1]/div[4]/div[1]/div[2]/div[" + i + "]/div/h2/a";
//            String title=xh.getTitle("vnxahoi.xml", xpathTitle);
//            System.out.println(i+"----Title:"+title);
//            
//            String xpathLink = "//*[@id=\"BodyWraper\"]/div/div[1]/div[4]/div[1]/div[2]/div[" + i + "]/div/h2/a";
//            String link = xh.getLink("vnxahoi.xml", xpathLink);
//            System.out.println("Link:" + link);
//
//            String xpathDate = "//*[@id=\"BodyWraper\"]/div/div[1]/div[4]/div[1]/div[2]/div[" + i + "]/div/p";
//            String date = xh.getDate("vnxahoi.xml", xpathDate);
//            System.out.println("----Date:" + date);
//
//            String xpathImgage="//*[@id=\"BodyWraper\"]/div/div[1]/div[4]/div[1]/div[2]/div["+i+"]/div/a/img";
//            String image=xh.getImage("vnxahoi.xml", xpathImgage);
//            System.out.println("----Image:"+image);
//
//            String xpathTomtat = "//*[@id=\"BodyWraper\"]/div/div[1]/div[4]/div[1]/div[2]/div[" + i + "]/div/h3";
//            String tomtat = xh.getTomtat("vnxahoi.xml", xpathTomtat);
//            System.out.println("----Tomtat:" + tomtat);
//        }

//    }
}

//    

//        //FileInputStream file = new FileInputStream(new File("index.xml"));
//        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
//        domFactory.setNamespaceAware(true); // never forget this!
//        DocumentBuilder builder = domFactory.newDocumentBuilder();
//        Document doc = builder.parse("index.xml");
//        //m thử copy xpath thẻ <div> coi   
//        XPathFactory factory = XPathFactory.newInstance();
//        XPath xPath = factory.newXPath();
//        XPathExpression xp = xPath.compile("//*[@id=\"BodyWraper\"]/div/div[1]/div[4]/div[1]/div[2]/div[4]/div/h2/a");
//        //xPath.setNamespaceContext(new MineralOccurrenceNamespaceContext());
//
//        Object kq = xp.evaluate(doc, XPathConstants.NODE);
//        Element nodes = (Element) kq;
//
//        System.out.println(nodes.getAttribute("href"));

        /*
         public String getLink() throws FileNotFoundException, ParserConfigurationException, SAXException, IOException {
         try {
         FileInputStream file = new FileInputStream(new File(this.xmlFile));

         DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

         DocumentBuilder builder = builderFactory.newDocumentBuilder();

         Document xmlDocument = builder.parse(file);

         javax.xml.xpath.XPath xPath = XPathFactory.newInstance().newXPath();
         String expression = this.link;
         Element node = (Element) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODE);
         String tmp = node.getAttribute("href");
         return tmp;
         } 
         catch (FileNotFoundException e) {
         } 
         catch (SAXException | ParserConfigurationException | XPathExpressionException e) {
         } 
         catch (IOException e) {
         }
         return null;
         */
